package com.group9.service.impl;

import com.group9.dto.FruitCartInfo;
import com.group9.pojo.*;
import com.group9.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Lcouncillor on 2020/5/14.
 */
@Service
public class FruitCartInfoServiceImpl implements FruitCartInfoService {

    @Autowired
    FruitCartService fruitCartService;
    @Autowired
    FruitInfoService fruitInfoService;
    @Autowired
    FruitPicService fruitPicService;
    @Autowired
    ActivityFruitService activityFruitService;
    @Autowired
    ActivityInfoService activityInfoService;

    /**
     * 通过用户id查询用户购物车相关的所有信息
     * @param customerId
     * @return
     */
    @Override
    @Transactional(readOnly = true)
    public List<FruitCartInfo> selectByCustomerId(Integer customerId) {
        List<FruitCart> fruitCarts = fruitCartService.selectCartByCustomerId(customerId);

        if (fruitCarts != null && !fruitCarts.isEmpty()){

            List<FruitCartInfo> fruitCartInfos = new ArrayList<FruitCartInfo>();

            for (FruitCart fruitCart:fruitCarts) {

                FruitCartInfo fruitCartInfo = new FruitCartInfo();
                fruitCartInfo.setCartId(fruitCart.getCartId());
                fruitCartInfo.setCustomerId(fruitCart.getCustomerId());
                fruitCartInfo.setFruitId(fruitCart.getFruitId());
                fruitCartInfo.setFruitAmount(fruitCart.getFruitAmount());

                FruitInfo fruitInfo = fruitInfoService.selectByFruitId(fruitCart.getFruitId());
                List<ActivityFruit> activityFruits = activityFruitService.selectByFruitId(fruitCart.getFruitId());
                FruitPic fruitPic = fruitPicService.selectByFruitIdAndPicType(fruitCart.getFruitId(), (byte)3);

                if ( fruitInfo == null || fruitInfo.getFruitStatus() == 0){
                    fruitCartInfo.setStatus((byte) 0);
                }else{
                    fruitCartInfo.setFruitName(fruitInfo.getFruitName());
                    fruitCartInfo.setFruitPrice(fruitInfo.getFruitPrice());
                    fruitCartInfo.setFruitActivityCost(fruitInfo.getFruitActivityCost());
                    fruitCartInfo.setFruitDescript(fruitInfo.getFruitDescript());
                    if ( null != fruitPic ){
                        fruitCartInfo.setPicUrl(fruitPic.getPicUrl());
                    }
                    fruitCartInfo.setStatus((byte) 1);

                    if ( null != activityFruits && !activityFruits.isEmpty()){
                        fruitCartInfo.setActivityId(activityFruits.get(0).getActivityId());
                        ActivityInfo activityInfo = activityInfoService.selectByActivityId(activityFruits.get(0).getActivityId());
                        fruitCartInfo.setActivityName(activityInfo.getActivityName());
                        fruitCartInfo.setStatus((byte) 2);
                    }

                }
                fruitCartInfos.add(fruitCartInfo);
            }
            return fruitCartInfos;
        }
        return null;
    }
}
